package com.yungu.swift.admin.route.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.base.CurrentUser;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.system.sys.model.dto.SysBusinessDto;
import com.yungu.swift.system.sys.model.dto.SysCompanyDto;
import com.yungu.swift.system.sys.model.dto.SysRegionalAgentDto;
import com.yungu.swift.system.sys.model.dto.SysWayCompanyDto;
import com.yungu.swift.system.sys.model.dto.SysWayDto;
import com.yungu.swift.system.sys.model.vo.AdminLoginVo;
import com.yungu.swift.system.sys.model.vo.BusinessVo;
import com.yungu.swift.system.sys.model.vo.CityVo;
import com.yungu.swift.system.sys.service.SysBusinessOrganizationService;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.system.sys.service.SysCompanyService;
import com.yungu.swift.system.sys.service.SysRegionalAgentService;
import com.yungu.swift.system.sys.service.SysWayCompanyService;
import com.yungu.swift.system.sys.service.SysWayService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/6/15
 */
@Slf4j
@RestController
@RequestMapping("/admin/v1/common/business")
@Api(value = "【通用服务】", tags = "系统业务类公共接口")
public class SystemAdmin {

    @Reference
    private SysBusinessOrganizationService sysBusinessOrganizationService;
    @Reference
    private SysRegionalAgentService sysRegionalAgentService;
    @Reference
    private SysCompanyService sysCompanyService;
    @Reference
    private SysCityService sysCityService;
    @Reference
    private SysWayService sysWayService;
    @Reference
    private SysWayCompanyService sysWayCompanyService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户开通的业务", httpMethod = "GET")
    public ResponseData<AdminLoginVo> list(@RequestBody AdminParam adminParam) {
        // 用户的业务列表
        String organizationUuid = getOrganizationUuid(adminParam);
        AdminLoginVo adminLoginVo = new AdminLoginVo();
        ResponseData<List<BusinessVo>> getBusiness = sysBusinessOrganizationService.getBusiness(MapUtils.build("organizationUuid", organizationUuid));
        if (getBusiness.isSuccess() && CollectionUtils.isNotEmpty(getBusiness.getData())) {
            List<BusinessVo> list = getBusiness.getData();
            adminLoginVo.setExpandBusinesses(list);
            // 跨城小件作为跨城拼车的扩展业务
            BusinessVo pancel = list.stream()
                    .filter(businessVo -> businessVo.getType() == CommonConstant.BUSINESS_TYPE_PARCEL)
                    .findFirst().orElse(null);
            if (pancel != null) {
                list.remove(pancel);
            }
            adminLoginVo.setBaseBusinesses(list);
        }
        return ResponseData.buildSuccessResponse(adminLoginVo);
    }

    @RequestMapping(value = "/agentByBusinessType", method = RequestMethod.GET)
    @ApiOperation(value = "根据业务类型查询代理数据", httpMethod = "GET")
    public ResponseData<List<SysRegionalAgentDto>> agentByBusinessType(@RequestParam String businessType,
                                                                       @RequestBody AdminParam adminParam) {
        if (ParamUtil.isIllegal(businessType)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        Map<String, Object> params = MapUtils.build(4);
        params.put("businessType", businessType);
        params.put("appid", adminParam.getAppid());
        params.put("organizationUuid", getOrganizationUuid(adminParam));
        return sysRegionalAgentService.findAgentByBusinessType(params);
    }

    @RequestMapping(value = "/companyByBusinessType", method = RequestMethod.GET)
    @ApiOperation(value = "根据业务、代理信息获取公司数据", httpMethod = "GET")
    public ResponseData<List<SysCompanyDto>> companyByBusinessType(@RequestParam String businessType,
                                                                   @RequestParam(required = false) String agentUuid,
                                                                   @RequestBody AdminParam adminParam) {
        if (ParamUtil.isIllegal(businessType)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        Map<String, Object> params = MapUtils.build();
        params.put("businessType", businessType);
        params.put("regionalAgentUuid", agentUuid);
        params.put("appid", adminParam.getAppid());
        params.put("organizationUuid", getOrganizationUuid(adminParam));
        return sysCompanyService.findCompanyByBusinessType(params);
    }

    @RequestMapping(value = "/cityByBusinessType", method = RequestMethod.GET)
    @ApiOperation(value = "根据业务、代理（或公司）信息获取城市数据", httpMethod = "GET")
    public ResponseData<List<CityVo>> findCityByBusinessType(@RequestParam(required = false) String businessType,
                                                             @RequestParam(required = false) String organizationUuid,
                                                             @RequestBody AdminParam adminParam) {
        Map<String, Object> params = MapUtils.build(4);
        params.put("businessType", businessType);
        params.put("organizationUuid", ParamUtil.defaultIfNull(organizationUuid, getOrganizationUuid(adminParam)));
        params.put("appid", adminParam.getAppid());
        return sysCityService.findCityByBusinessType(params);
    }

    @RequestMapping(value = "/wayByOrganization", method = RequestMethod.GET)
    @ApiOperation(value = "根据代理（或公司）信息获取已开通路线数据", httpMethod = "GET")
    public ResponseData<List<SysWayCompanyDto>> wayByOrganization(@RequestParam String organizationUuid) {
        return sysWayCompanyService.listWayByOrganization(organizationUuid);
    }


    @RequestMapping(value = "/allWayByOrganization", method = RequestMethod.GET)
    @ApiOperation(value = "根据代理信息获取路线数据", httpMethod = "GET")
    public ResponseData<List<SysWayDto>> allWayByOrganization(
            @ApiParam(value = "起终点 uuid") @RequestParam(required = false) String pointSiteUuids,
            @ApiParam(value = "代理uuid（企业管理时传）") @RequestParam(required = false) String agentUuid,
            @CurrentUser AdminParam adminParam) {
        Map<String, Object> build = MapUtils.build(4);
        build.put("appid", adminParam.getAppid());
        build.put("adminAgentUuid", agentUuid);
        if (StringUtils.isNotBlank(pointSiteUuids)) {
            build.put("pointSiteUuidList", Arrays.asList(StringUtils.split(pointSiteUuids, ",")));
        }
        return sysWayService.findWithOriginDest(build);
    }

    @RequestMapping(value = "/getBusiness", method = RequestMethod.GET)
    @ApiOperation(value = "根据代理（或公司）获取开通业务", httpMethod = "GET")
    public ResponseData<List<BusinessVo>> getBusiness(
            @ApiParam(required = true, value = "代理或者公司 uuid") @RequestParam String organizationUuid,
            @CurrentUser AdminParam adminParam) {
        Map<String, Object> params = MapUtils.build(4);
        params.put("organizationUuid", ParamUtil.defaultIfNull(organizationUuid, getOrganizationUuid(adminParam)));
        return sysBusinessOrganizationService.getBusiness(params);
    }

    @RequestMapping(value = "/getCityBusiness", method = RequestMethod.GET)
    @ApiOperation(value = "根据代理（或公司）、城市获取开通业务", httpMethod = "GET")
    public ResponseData<List<SysBusinessDto>> getCityBusiness(
            @ApiParam(required = true, value = "代理或者公司 uuid") @RequestParam String organizationUuid,
            @ApiParam(required = false, value = "城市 uuid") @RequestParam(required = false) String cityUuid,
            @CurrentUser AdminParam adminParam) {
        Map<String, Object> params = MapUtils.build(4);
        params.put("organizationUuid", ParamUtil.defaultIfNull(organizationUuid, getOrganizationUuid(adminParam)));
        params.put("cityUuid", cityUuid);
        return sysBusinessOrganizationService.getCityBusiness(params);
    }


    private String getOrganizationUuid(AdminParam adminParam) {
        String organizationUuid = null;
        if (StringUtils.isNotEmpty(adminParam.getAppid())) {
            organizationUuid = adminParam.getAppid();
        }
        if (StringUtils.isNotEmpty(adminParam.getUserAgentUuid())) {
            organizationUuid = adminParam.getUserAgentUuid();
        }
        if (StringUtils.isNotEmpty(adminParam.getUserCompanyUuid())) {
            organizationUuid = adminParam.getUserCompanyUuid();
        }
        return organizationUuid;
    }


}
